package com.credithc.signature.common.utils;

import com.credithc.signature.common.po.Channel;
import com.credithc.signature.common.po.Dictionary;
import com.credithc.signature.common.po.ErrorFilter;
import com.credithc.signature.common.po.OfficialSealTemplate;
import com.credithc.signature.common.po.SealChannel;
import com.credithc.signature.common.po.Template;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉18⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
public class CacheUtil {

    private static Cache<String, Object> cache;
    private static LoadingCache<String, Channel> channelCache;
    private static LoadingCache<String, Template> templateCache;
    private static LoadingCache<Object, List<OfficialSealTemplate>> templateSealLoadingCache;
    private static LoadingCache<Object, List<SealChannel>> channelSealLoadingCache;
    private static LoadingCache<Object, Dictionary> dictionaryLoadingCache;
    private static LoadingCache<String, List<ErrorFilter>> errorFilterLoadingCache;

    static {
        cache = CacheBuilder.newBuilder()
                .concurrencyLevel(8)
                .expireAfterWrite(3, TimeUnit.MINUTES)
                .build();
    }

    /**
     * 渠道缓存
     *
     * @return
     */
    public static LoadingCache<String, Channel> getChannelCache() {
        return CacheUtil.channelCache;
    }

    public static void setChannelCache(LoadingCache<String, Channel> channelCache) {
        CacheUtil.channelCache = channelCache;
    }

    /**
     * 模板缓存
     *
     * @return
     */
    public static LoadingCache<String, Template> getTemplateCache() {
        return CacheUtil.templateCache;
    }

    public static void setTemplateCache(LoadingCache<String, Template> templateCache) {
        CacheUtil.templateCache = templateCache;
    }

    /**
     * 模板公章缓存
     *
     * @param templateSealLoadingCache
     */
    public static void setTemplateSealLoadingCache(LoadingCache<Object, List<OfficialSealTemplate>> templateSealLoadingCache) {
        CacheUtil.templateSealLoadingCache = templateSealLoadingCache;
    }

    public static LoadingCache<Object, List<OfficialSealTemplate>> getTemplateSealLoadingCache() {
        return CacheUtil.templateSealLoadingCache;
    }

    /**
     * 渠道公章缓存
     */
    public static void setChannelSealLoadingCache(LoadingCache<Object, List<SealChannel>> channelSealLoadingCache) {
        CacheUtil.channelSealLoadingCache = channelSealLoadingCache;
    }

    public static LoadingCache<Object, List<SealChannel>> getChannelSealLoadingCache() {
        return CacheUtil.channelSealLoadingCache;
    }

    /**
     * 字典信息
     */
    public static void setDictionaryLoadingCache(LoadingCache<Object, Dictionary> dictionaryLoadingCache) {
        CacheUtil.dictionaryLoadingCache = dictionaryLoadingCache;
    }

    public static LoadingCache<Object, Dictionary> getDictionaryLoadingCache() {
        return CacheUtil.dictionaryLoadingCache;
    }

    /**
     * 错误信息
     *
     * @param errorFilterLoadingCache
     */
    public static void setErrorFilterLoadingCache(LoadingCache<String, List<ErrorFilter>> errorFilterLoadingCache) {
        CacheUtil.errorFilterLoadingCache = errorFilterLoadingCache;
    }

    public static LoadingCache<String, List<ErrorFilter>> getErrorFilterLoadingCache() {
        return CacheUtil.errorFilterLoadingCache;
    }

    /**
     * 获取缓存对象
     *
     * @return
     */
    public static Cache<String, Object> getCache() {
        return cache;
    }

    /**
     * @desction: 获取缓存
     */
    public static Object get(String key) {
        return StringUtils.isNotEmpty(key) ? cache.getIfPresent(key) : null;
    }

    /**
     * @desction: 获取缓存全部
     */
    public static Object getAll() {
        return cache.asMap();
    }

    /**
     * @desction: 放入缓存
     */
    public static void put(String key, Object value) {
        if (StringUtils.isNotEmpty(key) && value != null) {
            cache.put(key, value);
        }
    }

    /**
     * @desction: 移除缓存
     */
    public static void remove(String key) {
        if (StringUtils.isNotEmpty(key)) {
            cache.invalidate(key);
        }
    }

    /**
     * @desction: 批量删除缓存
     */
    public static void remove(List<String> keys) {
        if (keys != null && keys.size() > 0) {
            cache.invalidateAll(keys);
        }
    }

    /**
     * 清空缓存
     */
    public static void removeAll() {
        cache.invalidateAll();
    }

}
